package br.com.verde.exemploteca.lispy

object SetOperation {
  def member[A](x: A, xs: List[A]): Boolean = {
    if (xs.isEmpty) return false
    if (x == xs.head) return true
    member(x, xs.tail)
  }

  def inter[A](xs1: List[A],
      xs2: List[A],
      interList: List[A]): List[A] = {
    
    if (xs1.isEmpty) return interList
    
    if (member(xs1.head, xs2)) 
      return inter(xs1.tail, xs2, xs1.head :: interList)
      
    return inter(xs1.tail, xs2, interList)
  }

  def inter[A](xs1: List[A], xs2: List[A]): List[A] = inter(xs1, xs2, Nil)

  def main(args: Array[String]) {
    println(member(2, 1 :: 2 :: 3 :: Nil))
    println(member(7, 1 :: 2 :: 3 :: Nil))
    println(inter(1 :: 2 :: 3 :: 4 :: Nil, 2 :: 3 :: Nil));
    println(inter(1 :: 4 :: 7 :: Nil, 1 :: 2 :: 3 :: 4 :: Nil));
    println(inter(2 :: 3 :: Nil, 7 :: Nil));
    println(inter(2 :: 3 :: Nil, Nil));
  }
}